//
//  UDViewControllerTests.m
//  UnitDemo
//
//  Created by wangqinglu on 15/11/11.
//  Copyright © 2015年 xs. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "UDViewController.h"
#import "UDLoginManager.h"
#import <OCMock/OCMock.h>

@interface UDViewController (Test) {
   
}
@property(nonatomic, readwrite) UIButton* loginButton;
@property(nonatomic, readwrite) UDLoginManager* loginManager;

-(void) pressedLoginButton:(id)sender;
@end

@implementation UDViewController

@end

///////////////////////////
//@interface FakeUDLoginManager : UDLoginManager
////@property(nonatomic, readwrite) BOOL isLogged;
////-(void) signInWithUserName:(NSString*)username Password:(NSString*)psw;
//-(BOOL) isLoggedFake;
//@end

//@implementation UDLoginManager (Test)
//
//static BOOL g_isLoggedFake = NO;
//
//+(id) sharedInstance {
//    return OCMClassMock([UDLoginManager class]);
//}
//@end
/////////////////////////////

@interface UDViewControllerTests : XCTestCase {
    UDViewController* g_sut;
    CFRunLoopRef g_currentLoop;
    BOOL g_isTextChanged;
}

@end

@implementation UDViewControllerTests

- (void)setUp {
    [super setUp];
    
    // Put setup code here. This method is called before the invocation of each test method in the class.
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    g_sut = [storyboard instantiateViewControllerWithIdentifier:@"testViewController"];
    [g_sut performSelectorOnMainThread:@selector(loadViewIfNeeded) withObject:nil waitUntilDone:YES]; // 触发 viewDidLoad() 方法
    
    [g_sut.loginButton.titleLabel  addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [g_sut.loginButton.titleLabel removeObserver:self forKeyPath:@"text"];
    g_sut = nil;
    
    [super tearDown];
}

-(void) testLoginButtonShouldBeShowed {
    XCTAssertNotNil(g_sut.loginButton, @"登录按钮不能为空");
    NSArray *subviews = g_sut.view.subviews;
    XCTAssertTrue([subviews containsObject:g_sut.loginButton], @"按钮没有被添加到 ViewController 的 view 上");
}

-(void) testLoginButtonAction {
    NSArray* actions = [g_sut.loginButton actionsForTarget:g_sut forControlEvent:UIControlEventTouchUpInside];
    XCTAssertTrue([actions containsObject:@"pressedLoginButton:"], @"按钮点击事件需绑定到指定方法上");
}

-(void) testLoginButton_pressedLoginButton_callSignIn {
    g_sut.loginManager = OCMClassMock([UDLoginManager class]);
    [g_sut pressedLoginButton:nil];
    OCMVerify([g_sut.loginManager signInWithUserName:@"wql" Password:@"123456"]);
    g_sut.loginManager = [UDLoginManager sharedInstance]; // 恢复原单例对象，防止对其它测试用例造成影响
}

-(void) testLoginButton_updateAfterSuccessNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:UDLOGINMANAGER_NOTIFICATION_SIGNIN_SUCCESS object:nil userInfo:nil];
    g_currentLoop = CFRunLoopGetCurrent();
    
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    int count = 5;
    g_isTextChanged = NO;
    while (!g_isTextChanged && count > 0)
    {
        [runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
        count--;
    }
    NSLog(@"~~~%@", g_sut.loginButton.titleLabel.text);
    XCTAssertTrue([g_sut.loginButton.titleLabel.text isEqualToString:@"退出"]);
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void*)context {
    if([keyPath isEqualToString:@"text"])  {
        g_isTextChanged = YES;
    }
}

@end
